package com.siecom.transport.handler;

import android.os.Bundle;
import android.util.Log;

import com.siecom.algorithm.sm.SM2;
import com.siecom.algorithm.sm.SMUtil;
import com.siecom.constconfig.ConstDefine;
import com.siecom.constconfig.ErrorDefine;
import com.siecom.hostservice.MainActivity;
import com.siecom.module.DeviceCallback;
import com.siecom.module.DeviceInterface;
import com.siecom.proto.Message;
import com.siecom.tool.ByteTool;
import com.siecom.tool.LogUtil;
import com.siecom.transport.CommServer;
import com.siecom.transport.SiecomDevice;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * hardware encrypt
 */

public class KeyBoardHandler implements ServiceHandler {
    private static final String TAG = "KeyBoardHandler";
    private CommServer server;
    private int step = 0;
    private int passVoice = 0;
    private int inputTimes = 1;
    public static boolean plainTextInputing = true;
    private String tempMsgId = null;
    @Override
    public void onReceive(CommServer server, Message.siecomproto msg,String message) {
        Log.e("KeyBoardHandler", "onReceive");
        this.server = server;
        switch (msg.getCmd()){
            case ConstDefine.INPUT_MAIN_REQUEST:
                inputMainKey(msg);
                break;
            case ConstDefine.INPUT_WORK_REQUEST:
                inputWorkKey(msg);
                break;
            case ConstDefine.SM2_NEW_KEY_PAIR:
                sm2NewKeyPair(msg);
                break;
            case ConstDefine.INPUT_KEY_REQUEST:
                startInputPwd(msg);
                break;
            case ConstDefine.SET_ENCRYPT_REQUEST:
                setEncrypt(msg);
                break;
            case ConstDefine.KEYBOARD_CANCEL_REQUEST:
                cancelKeyBoard(msg);
                break;
//            case ConstDefine.TMK_REQUEST:
//                TMKOnKeyBoard(msg);
//                break;
            case ConstDefine.TMK_DECRYPT_REQUEST:
                calculateData(msg); //加解密数据
//                TMKDecrypt(msg);
                break;

            case ConstDefine.TMK_OLD_REQUEST:

//                TMKUPdate(msg);
                break;

            case ConstDefine.RESET_KEYBOARD_REQUEST:
                resetKeyBoard(msg);
                break;
            case ConstDefine.CALCMAC_REQUEST:
                calcMac(msg);
                break;
            case ConstDefine.CHECK_KEY_EXIST_REQUEST:
                checkKeysExist(msg);
                break;
        }
    }

    public void checkKeysExist(Message.siecomproto msg){
        DeviceInterface.checkKeysExist(new DeviceCallback() {
            @Override
            public void onStart() {

            }

            @Override
            public void onError(Bundle bundle) {

            }

            @Override
            public void onSuccess(Bundle bundle) {

            }

            @Override
            public void onCompleted(Bundle bundle) {

            }
        },false);
    }
    @Deprecated
    public  void calcMac(Message.siecomproto msg){
        JSONObject json = null;
        int MainKeyIndex = 0;
        int workKeyIndex  = 0;
        String data  = null;
        int macMode = 0x02;
        try {
            json = new JSONObject(msg.getRequest());
            MainKeyIndex = json.getInt("mainKeyIndex");
            workKeyIndex = json.getInt("workKeyIndex");
            data = json.getString("data");
            macMode = json.getInt("macMode");
        } catch (JSONException e) {
            e.printStackTrace();

        }
        byte[] macData = ByteTool.hexStr2Bytes(data);
        DeviceInterface.calcMAC(macMode, MainKeyIndex, workKeyIndex, macData, new DeviceCallback() {
            @Override
            public void onStart() {

            }

            @Override
            public void onError(Bundle bundle) {
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.KEY_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setRespond(bundle.getString("message"));
                msgBuilder.setError(bundle.getInt("code"));
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);

            }

            @Override
            public void onSuccess(Bundle bundle) {

                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.KEY_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setError(0);
                msgBuilder.setRespond(ByteTool.byte2hex(bundle.getByteArray("data")));
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);

            }

            @Override
            public void onCompleted(Bundle bundle) {

            }
        },false);

    }
   public  void resetKeyBoard(Message.siecomproto msg){
       JSONObject json = null;
       String keyData  = null;
       try {
           json = new JSONObject(msg.getRequest());
           keyData = json.getString("resetkeyData");
       } catch (JSONException e) {
           e.printStackTrace();

       }

       DeviceInterface.resetKeyBoardClear(new DeviceCallback() {
           @Override
           public void onStart() {

           }

           @Override
           public void onError(Bundle bundle) {

           }

           @Override
           public void onSuccess(Bundle bundle) {
               com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
               msgBuilder.setModule(ConstDefine.KEY_MODULE);
               msgBuilder.setMsgid(msg.getMsgid());
               msgBuilder.setCmd(msg.getCmd() + 1);
               msgBuilder.setError(0);
               com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
               server.sendToRemote(respondMsg);

           }

           @Override
           public void onCompleted(Bundle bundle) {



           }
       },keyData,false);

   }
    @Deprecated
    public void TMKUPdate(Message.siecomproto msg){

        JSONObject json = null;
        String keyIndex = null;
        String keyData  = null;
        try {
            json = new JSONObject(msg.getRequest());
            keyIndex = json.getString("keyIndex");
            keyData = json.getString("keyData");
        } catch (JSONException e) {
            e.printStackTrace();

        }

        DeviceInterface.updateMainKeyByOld(keyIndex, keyData, new DeviceCallback() {
            @Override
            public void onStart() {

            }
            @Override
            public void onError(Bundle bundle) {
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.KEY_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setRespond(bundle.getString("message"));
                msgBuilder.setError(bundle.getInt("code"));
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);

            }

            @Override
            public void onSuccess(Bundle bundle) {

                JSONObject json = new JSONObject();
                try {
                    json.put("data", bundle.getString("data"));
                }catch (JSONException e){

                }
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.KEY_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setError(0);
                msgBuilder.setRespond(json.toString());
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);

            }

            @Override
            public void onCompleted(Bundle bundle) {

            }
        },false);

    }

@Deprecated
    public void TMKDecrypt(Message.siecomproto msg){
        JSONObject json = null;
        String keyIndex = null;
        String keyData  = null;
        try {
            json = new JSONObject(msg.getRequest());
            keyIndex = json.getString("keyIndex");
            keyData = json.getString("keyData");
        } catch (JSONException e) {
            e.printStackTrace();

        }

        DeviceInterface.inputMainKeyByOld(keyIndex, keyData, new DeviceCallback() {
            @Override
            public void onStart() {


            }
            @Override
            public void onError(Bundle bundle) {
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.KEY_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setRespond(bundle.getString("message"));
                msgBuilder.setError(bundle.getInt("code"));
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);

            }

            @Override
            public void onSuccess(Bundle bundle) {

                JSONObject json = new JSONObject();
                try {
                    json.put("data", bundle.getString("data"));
                }catch (JSONException e){

                }
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.KEY_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setError(0);
                msgBuilder.setRespond(json.toString());
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);

            }

            @Override
            public void onCompleted(Bundle bundle) {

            }
        },false);


    }

    @Deprecated
    public void TMKOnKeyBoard(Message.siecomproto msg){
        Log.e("TMKOnKeyBoard","----");
        DeviceInterface.cancelKeyBoardInput();
        MainActivity.closeKeyBoard();

        step = 0;

        DeviceInterface.inputTMKOnBoard(new DeviceCallback() {
            @Override
            public void onStart() {

                Log.e("inputTMKOnBoard","onStart");
                MainActivity.showInputOne();

                step++;

            }

            @Override
            public void onError(Bundle bundle) {
                Log.e("inputTMKOnBoard","onError");
                MainActivity.closeKeyBoard("发生错误！");
                step = 0;
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(msg.getModule());
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setError(-1);
                msgBuilder.setRespond("input error");
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);
            }

            @Override
            public void onSuccess(Bundle bundle) {
                Log.e("inputTMKOnBoard","onSuccess");
                if(bundle.getInt("flag")==1) {
                    if (step == 1) {

                        MainActivity.showInputTwo();
                    }
                    if (step == 2) {

                        MainActivity.showInputThree();
                    }
                    if (step == 3) {

                        MainActivity.showInputFour();
                    }
                    if (step == 4) {
                        MainActivity.showInputFive();
                    }

                    step++;

                }else{


                    MainActivity.updateKeyBoard(bundle.getInt("data"));

                }



            }

            @Override
            public void onCompleted(Bundle bundle) {
                Log.e("inputTMKOnBoard","onCompleted"+step);
                if(step==6) {
                    com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                    msgBuilder.setModule(msg.getModule());
                    msgBuilder.setMsgid(msg.getMsgid());
                    msgBuilder.setCmd(msg.getCmd() + 1);
                    msgBuilder.setError(0);
                    com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                    server.sendToRemote(respondMsg);
                    MainActivity.closeKeyBoard("输入完成");
                    step=0;

                }


            }

        },false,60000);




    }


    public void cancelKeyBoard(Message.siecomproto msg){
        plainTextInputing = false;
        DeviceInterface.cancelKeyBoardInput();
        MainActivity.closeKeyBoard();
        com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
        msgBuilder.setModule(msg.getModule());
        msgBuilder.setMsgid(msg.getMsgid());
        msgBuilder.setCmd(msg.getCmd() + 1);
        msgBuilder.setError(0);
        msgBuilder.setRespond("cancelKeyBoard");
        com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
        server.sendToRemote(respondMsg);
        MainActivity.getmMainPci().DLL_PciGetPinExit();
        LogUtil.e(TAG,"DLL_PciGetPinExit--" );
//        MainActivity.getmMainPci().DLL_PciKbClose(); //
//        LogUtil.e(TAG,"DLL_PciKbClose" );
        MainActivity.getmSys().DLL_SysLedControl((byte) 0,(byte)0);

        if (tempMsgId != null){
            msgBuilder.setModule(msg.getModule());
            msgBuilder.setMsgid(tempMsgId);
            msgBuilder.setCmd(ConstDefine.INPUT_KEY_RESPOND);
            msgBuilder.setError(ErrorDefine.KEY_INPUT_CANCEL);
            msgBuilder.setRespond("cancelKeyBoard");
            com.siecom.proto.Message.siecomproto respondMsg2 = msgBuilder.build();
            server.sendToRemote(respondMsg2);
        }


    }
    @Deprecated
    public  void setEncrypt(Message.siecomproto msg){
        JSONObject json = null;
        try {
            json = new JSONObject(msg.getRequest());

            String endType = null;
            String mKeyIndex = null;
            String wKeyIndex = null;
            if(json.has("endType")){
                endType =   json.getString("endType");
            }
            if(json.has("mKeyIndex")){

                mKeyIndex = json.getString("mKeyIndex");
            }
            if(json.has("wKeyIndex")){
                wKeyIndex = json.getString("wKeyIndex");
            }

            DeviceInterface.keySetEncryptMode(json.getInt("encrypt"),endType,mKeyIndex,wKeyIndex, new DeviceCallback() {
                @Override
                public void onStart() {

                }

                @Override
                public void onError(Bundle bundle) {
                    com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                    msgBuilder.setModule(ConstDefine.KEY_MODULE);
                    msgBuilder.setMsgid(msg.getMsgid());
                    msgBuilder.setCmd(msg.getCmd() + 1);
                    msgBuilder.setRespond(bundle.getString("message"));
                    msgBuilder.setError(bundle.getInt("code"));
                    com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                    server.sendToRemote(respondMsg);
                }

                @Override
                public void onSuccess(Bundle bundle) {
                    com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                    msgBuilder.setModule(ConstDefine.KEY_MODULE);
                    msgBuilder.setMsgid(msg.getMsgid());
                    msgBuilder.setCmd(msg.getCmd() + 1);
                    msgBuilder.setError(0);
                    com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                    server.sendToRemote(respondMsg);

                }

                @Override
                public void onCompleted(Bundle bundle) {

                }
            },false);

        }catch (JSONException e) {
            e.printStackTrace();

        }

    }
    public  void  startInputPwd(Message.siecomproto msg){
        tempMsgId = msg.getMsgid();
        JSONObject json = null;
        String cardNo = null;
        String masterKeyIndex  = null;
        String workKeyIndex  = null;
        String timeOutSec = String.valueOf(msg.getTimeout()/1000);
        int encryptMode = 0;
        String pwdLength = null;
        try {
            json = new JSONObject(msg.getRequest());
            cardNo = json.getString("cardNo");
            if(json.has("mainKeyIndex"))
                masterKeyIndex = json.getString("mainKeyIndex");
            if(json.has("workKeyIndex"))
                workKeyIndex = json.getString("workKeyIndex");
            encryptMode = json.getInt("encryptType");
            LogUtil.e("----","加密模式：" + encryptMode);
            pwdLength = json.getString("pwdLength");
            if(json.has("passVoice")) {
                passVoice = json.getInt("passVoice");
            }
            if(json.has("inputTimes")) {
                inputTimes = json.getInt("inputTimes");
            }
            LogUtil.e(TAG,"可以调用 startInputPwd 了");
        } catch (JSONException e) {
            e.printStackTrace();
            LogUtil.e(TAG,"加密异常:" + e.getMessage());
        }
        DeviceInterface.startInputPwd(cardNo, masterKeyIndex, workKeyIndex, pwdLength, timeOutSec, encryptMode,inputTimes, new DeviceCallback() {
            @Override
            public void onStart() {
                MainActivity.getmSys().DLL_SysLedControl((byte) 0,(byte)1);
                MainActivity.showKeyBoard(passVoice,msg.getTimeout());
            }

            @Override
            public void onError(Bundle bundle) {
                MainActivity.getmMainPci().DLL_PciKbClose();
                LogUtil.e(TAG, "-------------onError == " + bundle.getInt("code"));
                if(bundle.getInt("code")== ErrorDefine.LENGTH_ERR){
                    MainActivity.keyBoardLengthERR();
                    return;
                }
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.KEY_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setRespond(bundle.getString("message"));
                int errorId = bundle.getInt("code");
                msgBuilder.setError(errorId);
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);
                MainActivity.closeKeyBoard();
                if (errorId == ErrorDefine.KEY_WEAK_ERR){
                    MainActivity.playVoice(ConstDefine.VOICE_PWD_TOO_SIMPLE);
                }else {
                    MainActivity.playVoice(ConstDefine.OPERATE_FAIL);
                }
            }

            @Override
            public void onSuccess(Bundle bundle) {
                int num  = bundle.getInt("data");
                int flag = bundle.getInt("flag");
                LogUtil.e(TAG,"num =" + num +  "-------------flag=" + flag);
                JSONObject json = new JSONObject();
                try {
                    json.put("data", num);
                    json.put("flag", flag);
                }catch (JSONException e){

                }
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.KEY_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setError(0);
                msgBuilder.setRespond(json.toString());
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);
                MainActivity.updateKeyBoard(bundle.getInt("data"));

            }

            @Override
            public void onCompleted(Bundle bundle) {
                String data  = bundle.getString("data");
                int flag = bundle.getInt("flag");
                LogUtil.e("onCompleted","num =" + data +  "-------------flag=" + flag);
                JSONObject json = new JSONObject();
                try {
                    json.put("flag", flag);
                    json.put("data", data);
                }catch (JSONException e){

                }
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.KEY_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setError(0);
                msgBuilder.setRespond(json.toString());
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);

                MainActivity.closeKeyBoard();

                MainActivity.playVoice(ConstDefine.OPERATE_SUCC);
            }

        },false);




    }

    private  void  inputMainKey(Message.siecomproto msg){
        JSONObject json = null;
        String keyIndex = null;
        String keyData  = null;
        int encryptType = 1;
        int mode = 0; //0x00明文写入  0x81 解密写入(更新密钥)  0x01加密写入
        try {
            json = new JSONObject(msg.getRequest());
            keyIndex = json.getString("keyIndex");
            keyData = json.getString("keyData");
            encryptType = json.getInt("encryptType");
            mode = json.getInt("mode");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        DeviceInterface.inputCipherMainKey(keyIndex, keyData,encryptType,mode, new DeviceCallback() {
            @Override
            public void onStart() {
            }
            @Override
            public void onError(Bundle bundle) {
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.KEY_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setRespond(bundle.getString("message"));
                msgBuilder.setError(bundle.getInt("code"));
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);

            }

            @Override
            public void onSuccess(Bundle bundle) {

                JSONObject json = new JSONObject();
                try {
                    json.put("data", bundle.getString("data"));
                }catch (JSONException e){

                }
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.KEY_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setError(0);
                msgBuilder.setRespond(json.toString());
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);


            }

            @Override
            public void onCompleted(Bundle bundle) {

            }
        },false);

    }


    private  void  inputWorkKey(Message.siecomproto msg){
        JSONObject json = null;
        String MainKeyIndex = null;
        String workKeyIndex  = null;
        String keyData  = null;
        int encryptType = 1;
        int mode = 0;
        int keyType = 1;
        try {
            json = new JSONObject(msg.getRequest());
            MainKeyIndex = json.optString("mainKeyIndex");
            workKeyIndex = json.optString("workKeyIndex");
            keyData = json.optString("keyData");
            encryptType = json.optInt("encryptType");
            mode = json.optInt("mode");
            keyType = json.optInt("keyType");
        } catch (JSONException e) {
            e.printStackTrace();

        }
        DeviceInterface.inputCipherWorkKey(MainKeyIndex,workKeyIndex,keyData,encryptType,mode,keyType, new DeviceCallback() {
            @Override
            public void onStart() {
            }
            @Override
            public void onError(Bundle bundle) {
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.KEY_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setRespond(bundle.getString("message"));
                msgBuilder.setError(bundle.getInt("code"));
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);

            }

            @Override
            public void onSuccess(Bundle bundle) {

                JSONObject json = new JSONObject();
                try {
                    json.put("data", bundle.getString("data"));
                }catch (JSONException e){

                }
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.KEY_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setError(0);
                msgBuilder.setRespond(json.toString());
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);

            }

            @Override
            public void onCompleted(Bundle bundle) {

            }
        },false);

    }

    //国密2生成密钥对
    private void sm2NewKeyPair(Message.siecomproto msg){
        DeviceInterface.getSM2keypair(new DeviceCallback() {
            @Override
            public void onStart() {

            }

            @Override
            public void onError(Bundle bundle) {
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.KEY_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setRespond(bundle.getString("message"));
                msgBuilder.setError(bundle.getInt("code"));
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);
            }

            @Override
            public void onSuccess(Bundle bundle) {

                JSONObject json = new JSONObject();
                try {
                    json.put("data", bundle.getString("data"));
                }catch (JSONException e){

                }
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.KEY_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setError(0);
                msgBuilder.setRespond(json.toString());
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);
            }

            @Override
            public void onCompleted(Bundle bundle) {

            }
        });
    }


    /**
     * 计算加解密数据
     * @param msg
     */
    private  void  calculateData(Message.siecomproto msg){
        JSONObject json = null;
        String keyIndex = null;
        String keyData  = null;
        int encryptType = 1;
        int mode = 0; //0 加密  1 解密
        int enMode = 1;
        try {
            json = new JSONObject(msg.getRequest());
            keyIndex = json.optString("keyIndex");
            keyData = json.optString("keyData");

            encryptType = json.optInt("encryptType");
            mode = json.optInt("mode");
            enMode = json.optInt("enMode"); // ECB CBC
        } catch (JSONException e) {
            e.printStackTrace();
        }

        DeviceInterface.calculateData(Integer.parseInt(keyIndex),mode,encryptType, keyData,(byte) enMode, new DeviceCallback() {
            @Override
            public void onStart() {
            }
            @Override
            public void onError(Bundle bundle) {
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.KEY_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setRespond(bundle.getString("message"));
                msgBuilder.setError(bundle.getInt("code"));
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);

            }

            @Override
            public void onSuccess(Bundle bundle) {

                JSONObject json = new JSONObject();
                try {
                    json.put("data", bundle.getString("data"));
                }catch (JSONException e){

                }
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.KEY_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setError(0);
                msgBuilder.setRespond(json.toString());
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);


            }

            @Override
            public void onCompleted(Bundle bundle) {

            }
        },false);

    }


}
